package com.yuanfeng.userms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.LimitUtils;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.PasswordUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import com.yuanfeng.userms.entity.UserAdminBaseEntity;
import com.yuanfeng.userms.feign.PaymentServiceClient;
import com.yuanfeng.userms.mapper.UserAdminBaseMapper;
import com.yuanfeng.userms.service.UserAdminBaseService;
import com.yuanfeng.userms.vo.UserPersonalBaseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

/**
 * @ClassName UserAdminBaseServiceImpl
 * @Description  后台用户基础信息表
 * @Author ty
 * @Date 2023-06-27 14:05:30
 * @Version 1.0
 **/
@Service
public class UserAdminBaseServiceImpl extends ServiceImpl<UserAdminBaseMapper, UserAdminBaseEntity>
        implements UserAdminBaseService {

    @Autowired
    private PaymentServiceClient paymentServiceClient;
    /**
     * 获取用户信息
     */
    @Override
    public ResponseResult getLimit(Map<String, String> paraMap) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(paraMap.get("token"));
        if (userInfo == null ) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);
        }
        return ResponseResult.success(userInfo);
    }

    @Override
    public ResponseResult deleteAdminUserPass(UserPersonalBaseVO param) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(param.getToken());
        if (null != userInfo) {
            this.baseMapper.delete(new LambdaQueryWrapper<UserAdminBaseEntity>()
                    .eq(UserAdminBaseEntity::getUserId, param.getUserId()));
            return ResponseResult.success();
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL.getCode(), BizCodeEnume.TOKEN_IS_ILLEGAL.getMsg());
    }

    @Override
    public ResponseResult getPersonalBase(UserPersonalBaseVO param) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(param.getToken());
        if (null != userInfo) {
            UserAdminBaseEntity personalBase = this.baseMapper.selectById(userInfo.getUserId());
            return ResponseResult.success(personalBase);
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult getPlatformUserInfo(Map<String, String> map) throws IOException {
        UserInfoVO platformUserInfo = this.baseMapper.getPlatformUserInfo(map);
        return ResponseResult.success(platformUserInfo);
    }

    @Override
    public ResponseResult getDistrictChildIds(String userId) {
        String districtChildIds = this.baseMapper.getDistrictChildIds(userId);
        return ResponseResult.success(districtChildIds);
    }

    @Override
    public ResponseResult addAdminUser(Map<String, String> param) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(param.get("token"));
        if (null != userInfo) {
            Boolean torf = LimitUtils.checkLimit("authority", userInfo);
            if (!torf) {
                return ResponseResult.fail(BizCodeEnume.INSUFFICIENT_PERMISSIONS);
            }
            //判断添加用户名 是否重复
            if (this.baseMapper.selectCount(new LambdaQueryWrapper<UserAdminBaseEntity>().eq(UserAdminBaseEntity::getUserAccount,param.get("userAccount"))) >0) {
                return ResponseResult.fail(BizCodeEnume.ADD_ADMIN_USER_ERROR);
            }
            //密码加密
            param.put("password",PasswordUtils.BCryptPasswordEncoder(param.get("password")));
            this.baseMapper.addAdminUser(param);
            String userId = this.baseMapper.selectOne(new LambdaQueryWrapper<UserAdminBaseEntity>()
                            .eq(UserAdminBaseEntity::getUserAccount,param.get("userAccount"))).getUserId();
            //在payment库中添加用户信息
            paymentServiceClient.createUser(userId);
            return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult updateAdminUserPass(Map<String, String> param) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(param.get("token"));
        if (null != userInfo) {
            if (param.get("password") != null && !"".equals(param.get("password").trim())) {
                //密码加密
                param.put("password",PasswordUtils.BCryptPasswordEncoder(param.get("password")));
            }
            this.baseMapper.updateAdminUser(param);
            return ResponseResult.success();
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult queryAdminUserList(UserPersonalBaseVO param) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(param.getToken());
        if (null != userInfo) {
            Page<UserPersonalBaseVO> page = new Page<>(param.getPage(), param.getLimit());
            IPage<UserPersonalBaseVO> results = this.baseMapper.queryAdminUserList(page, param);
            return ResponseResult.success(results);
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }
}
